package com.bt.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bt.annotation.RequiresPermissionsDesc;
import com.bt.pojo.DtsCoupon;
import com.bt.pojo.DtsCouponUser;
import com.bt.service.DtsCouponService;
import com.bt.service.DtsCouponUserService;
import com.bt.util.JacksonUtil;
import com.bt.util.ResponseUtil;
import com.qcloud.cos.utils.Jackson;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wbt
 * @version v1.0
 * @project shop_manage
 * @data 2022/9/26 10:28
 **/
@RequestMapping("/admin/coupon")
@CrossOrigin
@RestController
public class AdminCouponController {
    @Autowired
    private DtsCouponService dtsCouponServiceImpl;
    @Autowired
    private DtsCouponUserService dtsCouponUserServiceImpl;
    /**
     * http://localhost:8080/admin/coupon/list?page=1&limit=20&name=11&type=2&status=0&sort=add_time&order=desc
     * get
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:coupon:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "推广管理"}, button = "查询优惠券")
    public Object list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer limit, String name, Short type, Short status, String sort, String order){
        if (page<1){
            page=1;
        }
        if (limit<1){
            limit=20;
        }
        IPage<DtsCoupon> IPage= dtsCouponServiceImpl.findDtsCouponByPage(page,limit,name,type,status,sort,order);
        Map data=new HashMap<>();
        data.put("total",IPage.getTotal());
        data.put("items",IPage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * http://localhost:8080/admin/coupon/read?id=1
     * get
     */
    @GetMapping("/read")
    @RequiresPermissions("admin:coupon:read")
    @RequiresPermissionsDesc(menu = {"系统管理", "推广管理"}, button = "查看优惠价")
    public Object read(Integer id){
        DtsCoupon dtsCoupon=dtsCouponServiceImpl.findDtsCouponById(id);
        return ResponseUtil.ok(dtsCoupon);
    }
    /**
     * http://localhost:8080/admin/coupon/listuser?page=1&limit=20&couponId=1&userId=1&status=2&sort=add_time&order=desc
     * get
     */
    @GetMapping("/listuser")
    @RequiresPermissions("admin:coupon:listuser")
    @RequiresPermissionsDesc(menu = {"系统管理", "推广管理"}, button = "查询优惠券用户")
    public Object listuser(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer limit, Integer couponId, Integer userId, Short status, String sort, String order){
        if (page<1){
            page=1;
        }
        if (limit<1){
            limit=20;
        }
        IPage<DtsCouponUser> IPage= dtsCouponUserServiceImpl.findDtsCouponUserByPage(page,limit,couponId,userId,status,sort,order);
        Map data=new HashMap<>();
        data.put("total",IPage.getTotal());
        data.put("items",IPage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * http://localhost:8080/admin/coupon/create
     * post
     */
    @PostMapping("/create")
    @RequiresPermissions("admin:coupon:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "推广管理"}, button = "添加优惠券")
    public Object create(@RequestBody DtsCoupon dtsCoupon){
        if (StringUtils.isEmpty(dtsCoupon.getName())){
            return ResponseUtil.badArgument();
        }
        try {
            dtsCouponServiceImpl.insertDtsCoupon(dtsCoupon);
            return ResponseUtil.ok(dtsCoupon);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }
    }
    /**
     * http://localhost:8080/admin/coupon/delete
     * post
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:coupon:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "推广管理"}, button = "删除优惠券")
    public Object delete(@RequestBody DtsCoupon dtsCoupon){
        if (dtsCoupon.getId()==null){
            return ResponseUtil.badArgument();
        }
        try {
            dtsCouponServiceImpl.deleteDtsCouponById(dtsCoupon.getId());
            return ResponseUtil.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }
    }
/**
     * http://localhost:8080/admin/coupon/update
     * post
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:coupon:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "推广管理"}, button = "修改优惠券")
    public Object update(@RequestBody DtsCoupon dtsCoupon){
        if (dtsCoupon.getId()==null){
            return ResponseUtil.badArgument();
        }
        try {
            dtsCouponServiceImpl.updateDtsCouponById(dtsCoupon);
            return ResponseUtil.ok(dtsCoupon);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }
    }


}
